{% extends 'base.html' %}
{% load static %}

{% block title %}我的订单 - 商城系统{% endblock %}

{% block extra_css %}
<style>
    .order-container {
        background-color: #f8f9fa;
        padding: 30px 0;
        min-height: 70vh;
    }
    
    .order-card {
        background-color: #fff;
        border-radius: 8px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        margin-bottom: 20px;
    }
    
    .order-header {
        padding: 15px 20px;
        border-bottom: 1px solid #eee;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    
    .order-header .order-date {
        color: #666;
        font-size: 14px;
    }
    
    .order-body {
        padding: 15px 20px;
    }
    
    .order-item {
        display: flex;
        margin-bottom: 10px;
        padding-bottom: 10px;
        border-bottom: 1px solid #f5f5f5;
    }
    
    .order-item:last-child {
        margin-bottom: 0;
        padding-bottom: 0;
        border-bottom: none;
    }
    
    .order-item-img {
        width: 80px;
        height: 80px;
        object-fit: cover;
        border-radius: 4px;
    }
    
    .order-item-info {
        flex: 1;
        padding: 0 15px;
    }
    
    .order-item-name {
        font-size: 16px;
        margin-bottom: 5px;
    }
    
    .order-item-meta {
        color: #666;
        font-size: 14px;
    }
    
    .order-item-price {
        font-weight: bold;
        color: #e4393c;
    }
    
    .order-footer {
        padding: 15px 20px;
        border-top: 1px solid #eee;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #fafafa;
    }
    
    .order-total {
        font-size: 16px;
    }
    
    .order-total-price {
        color: #e4393c;
        font-weight: bold;
        font-size: 18px;
    }
    
    .order-actions {
        display: flex;
        gap: 10px;
    }
    
    .status-badge {
        display: inline-block;
        padding: 3px 8px;
        border-radius: 3px;
        font-size: 12px;
        font-weight: 600;
    }
    
    .order-tabs .nav-link {
        color: #495057;
        border: 1px solid transparent;
        border-top-left-radius: .25rem;
        border-top-right-radius: .25rem;
        padding: .5rem 1rem;
        cursor: pointer;
    }
    
    .order-tabs .nav-link.active {
        color: #0d6efd;
        background-color: #fff;
        border-color: #dee2e6 #dee2e6 #fff;
        border-bottom-width: 2px;
    }
    
    .empty-order {
        padding: 40px;
        text-align: center;
    }
    
    .empty-order i {
        font-size: 48px;
        color: #d0d0d0;
        margin-bottom: 20px;
    }
    
    .star-rating {
        display: flex;
        flex-direction: row-reverse;
        font-size: 24px;
    }
    
    .star-rating input {
        display: none;
    }
    
    .star-rating label {
        cursor: pointer;
        color: #ccc;
        padding: 0 5px;
    }
    
    .star-rating label.active,
    .star-rating input:checked ~ label,
    .star-rating label:hover,
    .star-rating label:hover ~ label {
        color: #f8ce0b;
    }
</style>
{% endblock %}

{% block content %}
<div class="order-container">
    <div class="container">
        <h2 class="mb-4">我的订单</h2>
        
        <!-- 订单状态导航 -->
        <ul class="nav nav-tabs mb-4 order-tabs">
            <li class="nav-item">
                <a class="nav-link {% if not request.GET.status %}active{% endif %}" href="{% url 'order_list' %}">全部订单</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if request.GET.status == 'PENDING' %}active{% endif %}" href="{% url 'order_list' %}?status=PENDING">待付款</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if request.GET.status == 'PAID' %}active{% endif %}" href="{% url 'order_list' %}?status=PAID">待发货</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if request.GET.status == 'SHIPPING' %}active{% endif %}" href="{% url 'order_list' %}?status=SHIPPING">配送中</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if request.GET.status == 'COMPLETED' %}active{% endif %}" href="{% url 'order_list' %}?status=COMPLETED">已完成</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if request.GET.status == 'CANCELLED' %}active{% endif %}" href="{% url 'order_list' %}?status=CANCELLED">已取消</a>
            </li>
        </ul>
        
        {% if orders %}
            {% for order in orders %}
            <div class="order-card">
                <div class="order-header">
                    <div>
                        <span class="order-id">订单号: {{ order.order_id }}</span>
                        <span class="order-date">{{ order.created_time|date:"Y-m-d H:i" }}</span>
                    </div>
                    <div>
                        <span class="status-badge bg-{{ order.order_status_display_color }}">
                            {{ order.get_order_status_display }}
                        </span>
                    </div>
                </div>
                
                <div class="order-body">
                    {% for item in order.items.all %}
                    <div class="order-item">
                        <img src="{{ item.product.get_main_image_url }}" alt="{{ item.product.name }}" class="order-item-img">
                        <div class="order-item-info">
                            <h5 class="order-item-name">{{ item.product.name }}</h5>
                            <div class="order-item-meta">
                                <span class="order-item-price">¥{{ item.price }}</span>
                                <span class="order-item-quantity">x{{ item.quantity }}</span>
                            </div>
                        </div>
                        <div class="order-item-total text-end">
                            <div class="order-item-price">¥{{ item.total_price }}</div>
                        </div>
                    </div>
                    {% endfor %}
                </div>
                
                <div class="order-footer">
                    <div class="order-total">
                        总计: <span class="order-total-price">¥{{ order.actual_amount }}</span>
                        <span class="text-muted small">(含运费: ¥{{ order.shipping_fee }})</span>
                    </div>
                    
                    <div class="order-actions">
                        <a href="{% url 'order_detail' order_id=order.order_id %}" class="btn btn-outline-primary btn-sm">订单详情</a>
                        
                        {% if order.order_status == 'PENDING' %}
                            <a href="{% url 'payment' order_id=order.order_id %}" class="btn btn-primary btn-sm">去支付</a>
                            <a href="{% url 'cancel_order' order_id=order.order_id %}" 
                               class="btn btn-outline-danger btn-sm"
                               onclick="return confirm('确定要取消此订单吗？')">取消订单</a>
                        {% elif order.order_status == 'SHIPPING' %}
                            <a href="{% url 'confirm_receipt' order_id=order.order_id %}"
                               class="btn btn-success btn-sm"
                               onclick="return confirm('确定已收到商品吗？')">确认收货</a>
                        {% elif order.order_status == 'COMPLETED' %}
                            {% with show_comment_btn=True %}
                                {% for item in order.items.all %}
                                    {% if item.comment_status == 'NOT_COMMENTED' and show_comment_btn %}
                                        <a href="#" class="btn btn-outline-secondary btn-sm comment-btn" data-order-item-id="{{ item.id }}" data-product-name="{{ item.product.name }}" data-bs-toggle="modal" data-bs-target="#commentModal">评价</a>
                                        {% with show_comment_btn=False %}{% endwith %}
                                    {% endif %}
                                {% endfor %}
                            {% endwith %}
                            <a href="{% url 'rebuy_order' order_id=order.order_id %}" class="btn btn-outline-primary btn-sm">再次购买</a>
                        {% endif %}
                    </div>
                </div>
            </div>
            {% endfor %}
            
            <!-- 分页导航 -->
            {% if page_obj.paginator.num_pages > 1 %}
            <nav aria-label="订单分页" class="mt-4">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?{% if current_status %}status={{ current_status }}&{% endif %}page={{ page_obj.previous_page_number }}">上一页</a>
                    </li>
                    {% else %}
                    <li class="page-item disabled">
                        <span class="page-link">上一页</span>
                    </li>
                    {% endif %}
                    
                    {% for i in page_obj.paginator.page_range %}
                        {% if page_obj.number == i %}
                        <li class="page-item active">
                            <span class="page-link">{{ i }}</span>
                        </li>
                        {% elif i > page_obj.number|add:'-3' and i < page_obj.number|add:'3' %}
                        <li class="page-item">
                            <a class="page-link" href="?{% if current_status %}status={{ current_status }}&{% endif %}page={{ i }}">{{ i }}</a>
                        </li>
                        {% endif %}
                    {% endfor %}
                    
                    {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?{% if current_status %}status={{ current_status }}&{% endif %}page={{ page_obj.next_page_number }}">下一页</a>
                    </li>
                    {% else %}
                    <li class="page-item disabled">
                        <span class="page-link">下一页</span>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        {% else %}
            <div class="empty-order">
                <i class="bi bi-inbox"></i>
                <h3>暂无订单</h3>
                <p class="text-muted">您还没有任何订单记录</p>
                <a href="{% url 'index' %}" class="btn btn-primary mt-3">去购物</a>
            </div>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block extra_content %}
<!-- 评价模态框 -->
<div class="modal fade" id="commentModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">评价商品</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="commentForm" method="post" action="">
                {% csrf_token %}
                <div class="modal-body">
                    <h6 id="productName" class="mb-3"></h6>
                    
                    <div class="mb-3">
                        <label class="form-label">评分</label>
                        <div class="rating-stars">
                            <div class="d-flex">
                                <div class="star-rating">
                                    {% for i in "12345" %}
                                    <input type="radio" id="star{{ i }}" name="rating" value="{{ i }}" {% if forloop.last %}checked{% endif %}>
                                    <label for="star{{ i }}"><i class="bi bi-star-fill"></i></label>
                                    {% endfor %}
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label for="commentContent" class="form-label">评价内容</label>
                        <textarea class="form-control" id="commentContent" name="content" rows="3" required></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">提交评价</button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 评价模态框事件处理
        const commentBtns = document.querySelectorAll('.comment-btn');
        const commentForm = document.getElementById('commentForm');
        const productNameElement = document.getElementById('productName');
        
        commentBtns.forEach(btn => {
            btn.addEventListener('click', function() {
                const orderItemId = this.getAttribute('data-order-item-id');
                const productName = this.getAttribute('data-product-name');
                
                // 设置表单提交地址
                commentForm.action = `/orders/comment/${orderItemId}/`;
                
                // 设置商品名称
                productNameElement.textContent = productName;
            });
        });
        
        // 星级评分样式
        const ratingInputs = document.querySelectorAll('.star-rating input');
        const ratingLabels = document.querySelectorAll('.star-rating label');
        
        ratingInputs.forEach((input, index) => {
            input.addEventListener('change', function() {
                // 更新星星显示
                ratingLabels.forEach((label, labelIndex) => {
                    if (labelIndex <= index) {
                        label.classList.add('active');
                    } else {
                        label.classList.remove('active');
                    }
                });
            });
        });
    });
</script>
{% endblock %} 